package com.genersoft.iot.vmp.storager.dao;

import java.util.List;

import com.genersoft.iot.vmp.gb28181.bean.MobilePosition;
import org.apache.ibatis.annotations.*;
//import org.springframework.stereotype.Repository;

@Mapper
//@Repository
public interface DeviceMobilePositionMapper {

    @Insert("INSERT INTO device_mobile_position (device_id, device_name, time, longitude, latitude, altitude, speed, direction, report_source, geodetic_system, cn_lng, cn_lat) " +
            "VALUES ('${deviceId}', '${deviceName}', '${time}', ${longitude}, ${latitude}, ${altitude}, ${speed}, ${direction}, '${reportSource}', '${geodeticSystem}', '${cnLng}', '${cnLat}')")
    int insertNewPosition(MobilePosition mobilePosition);

    @Select(value = {" <script>" +
    "SELECT * FROM device_mobile_position" +
    " WHERE device_id = #{deviceId} " +
    "<if test=\"startTime != null\"> AND time&gt;=to_date(#{startTime},'yyyy-MM-dd HH24:mi:ss')</if>" +
    "<if test=\"endTime != null\"> AND time&lt;=to_date(#{endTime},'yyyy-MM-dd HH24:mi:ss')</if>" +
    " ORDER BY time ASC" +
    " </script>"})
    @Results(id="mobilePosition",value={
    	@Result(property="deviceId",column="device_id")	,@Result(property="deviceName",column="device_name")	,
    	@Result(property="time",column="time")	,@Result(property="longitude",column="longitude")	,
    	@Result(property="latitude",column="latitude")	,@Result(property="altitude",column="altitude")	,
    	@Result(property="speed",column="speed")	,@Result(property="direction",column="direction")	,
    	@Result(property="reportSource",column="report_source")	,@Result(property="GeodeticSystem",column="geodetic_system")	,
    	@Result(property="cnLng",column="cn_lng")	,@Result(property="cnLat",column="cn_lat")	,
    		
    })
    List<MobilePosition> queryPositionByDeviceIdAndTime(String deviceId, String startTime, String endTime);

    @Select("SELECT * FROM device_mobile_position WHERE device_id = #{deviceId} and ROWNUM = 1 " +
            " ORDER BY time DESC")
    @ResultMap("mobilePosition")
    MobilePosition queryLatestPositionByDevice(String deviceId);

    @Delete("DELETE FROM device_mobile_position WHERE device_id = #{deviceId}")
    int clearMobilePositionsByDeviceId(String deviceId);

}
